//===============================================================================
// Q4Tech Engineering Team
// Mobile Updater Application Block - November 2006
//===============================================================================
// Copyright  Q4Tech Informatica Argentina.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace Microsoft.ApplicationBlocks.Mobile.Updater.Evaluators
{
    /// <summary>
    /// Evaluates wheter it is possible to install an application
    /// based on available device space.
    /// The storageSpaceNeeded format is
    /// NNX
    /// where NN is a decimal number 
    /// X is a unit of measure: K (Kb) or M (Meg)
    /// </summary>
    public class AvailableStorage : IConditionEvaluator
    {
        private string storageSpaceNeeded;
        private bool sotrageCard;

        #region IConditionEvaluator Members

        public bool Evaluate()
        {
            string unit = storageSpaceNeeded.Substring(storageSpaceNeeded.Length - 1, 1);

            double value = 0.0;

            try
            {
                value = double.Parse(storageSpaceNeeded.Substring(0, storageSpaceNeeded.Length - 1).Trim());
            }
            catch
            {
                throw new NotImplementedException(String.Format("{0} is not recognized", storageSpaceNeeded));
            }

            double multiplier = 0;

            switch (unit)
            {
                case "K":
                    multiplier = 1024;
                    break;
                case "M":
                    multiplier = 1024 * 1024;
                    break;
                default:
                    throw new NotImplementedException(String.Format("{0} is not recognized", storageSpaceNeeded));
            }

            value *= multiplier;

            if (sotrageCard)
            {
                foreach (String storageCard in DeviceStatusNativeHelper.GetStorageCardNames())
                {
                    if (DeviceStatusNativeHelper.GetFreeBytes(storageCard) >= value)
                        return true;
                }

                return false;
            }
            else
            {
                return DeviceStatusNativeHelper.GetFreeBytes("\\") >= value;
            }
        }

        public String RejectReason
        {
            get
            {
                return "Not enough space in the storage.";
            }
        }

        public void Init(Microsoft.ApplicationBlocks.Mobile.Updater.Xsd.ConditionEvaluatorProviderData data, Microsoft.ApplicationBlocks.Mobile.Updater.Manifest manifest)
        {
            foreach (XmlAttribute atrib in data.AnyAttr)
            {
                if (atrib.Name == "space")
                    this.storageSpaceNeeded = atrib.Value;

                if (atrib.Name == "storageCard")
                    this.sotrageCard = bool.Parse(atrib.Value);
            }

        }

        #endregion
    }
}
